//
//  HYFWelcomeController.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/22.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit


class HYFWelcomeController: UIViewController {
    
    override func loadView() {
        
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        //改变label 的 约束
        self.headImageView.snp_updateConstraints(closure: { (make) in
            make.top.equalTo(self.view).offset(100)
        })
        
        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
            
            //对页面进行刷新
            self.view.layoutIfNeeded()
            
            }) { (_) in
            
                UIView.animateWithDuration(0.25, animations: { 
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) in
                      
                    //发送通知到 appdelegate, 切换根控制器
                    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "welcome")
                        
                        
                })
        }
    }
    
    
    func setupUI(){
        
        //添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        //设置frame
        headImageView.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
            
        }
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
    }
    
    //懒加载数据
    private lazy var bgImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var headImageView:UIImageView = {
        
        let img = UIImageView(imgName: "avatar_default_big")
        
        img.hyf_setImage(HYFUserAccountViewModel.sharedTools.userAccount?.avatar_large, placeholderImgName: "avatar_default_big")
        img.layer.borderColor = UIColor.orangeColor().CGColor
        img.layer.borderWidth = 2
        img.layer.cornerRadius = 45
        img.layer.masksToBounds = true
        
        return img
    }()
    
    //欢迎回来
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(18)
        label.text = "欢迎回来"
        label.textAlignment = .Center
        label.alpha = 0
        
        return label
    }()

  
}
